package com.example.administrator.hello;

import android.content.Context;
import android.content.Intent;
import android.provider.Settings;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import android.support.test.uiautomator.By;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject2;
import android.support.test.uiautomator.Until;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.assertEquals;

/**
 * Instrumentation test, which will execute on an Android device.
 *
 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
 */
@RunWith(AndroidJUnit4.class)
public class kaiTest {
    private UiDevice device;
    final String PACKAGE_NAME="net.oschina.app";

    /**
     * @throws Exception
     */
//    @Test
//    public void name() throws Exception {
//        device.findObjects(By.res("")).get(3);
//        UiObject2 buttontext1=device.findObject(By.res("net.oschina.app:id/nav_item_me"));
//        buttontext1.click();
//        Thread.sleep(3000);
//        UiObject2 buttontext2=device.findObject(By.res("net.oschina.app:id/iv_portrait"));
//        buttontext2.click();
//        Thread.sleep(3000);
//        UiObject2 firstNumber=device.findObject(By.res("net.oschina.app:id/et_login_username"));
//        firstNumber.setText("10");
//        UiObject2 secondNumber=device.findObject(By.res("net.oschina.app:id/et_login_pwd"));
//        secondNumber.setText("20");
//        Thread.sleep(3000);
//        UiObject2 buttontext=device.findObject(By.res("net.oschina.app:id/bt_login_submit"));
//        buttontext.click();
//        UiObject2 resulttext=device.findObject(By.res("com.example.administrator.hello:id/result"));
//        String actualValue=resulttext.getText().toString();
//        String expectValue="30";
//        assertEquals(expectValue,actualValue);


    @Before
    public void setUp() throws Exception {
        // 实例化UiDevice
        device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
        // 启动应用
        //获取上下文
        Context context = InstrumentationRegistry.getContext();
        //通过将包名传给包管理器获取启动的intent
        final Intent intent = context.getPackageManager()
                .getLaunchIntentForPackage(PACKAGE_NAME);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        //启动应用
        context.startActivity(intent);
        // 等待应用启动
        device.wait(Until.hasObject(By.pkg(PACKAGE_NAME).depth(0)),
                10000);
        Thread.sleep(3000);
        device.wait(Until.hasObject(By.res("net.oschina.app:id/tv_title")),10000);




    }

    @Test
    public void useAppContext() throws Exception {
        UiObject2 title = device.findObjects(By.res("net.oschina.app:id/tv_title")).get(1);

        System.out.println("==========="+title.getText().toString());

    }

    @After
    public void tearDown() throws Exception {


    }






}
